export function getRequest(url) {
	//获取url中"?"符后的字串  
	const theRequest = new Object();
	if (url.indexOf("?") != -1) {
		const str = url.substr(1);
		const strs = str.split("&");
		for (let i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
		}
	}
	return theRequest;
}

export function format(dataString) {
	//dataString是整数，否则要parseInt转换
	var time = new Date(dataString);
	var year = time.getFullYear();
	var month = time.getMonth() + 1;
	var day = time.getDate();
	var hour = time.getHours();
	var minute = time.getMinutes();
	var second = time.getSeconds();
	return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + (hour < 10 ? '0' +
		hour : hour) + ':' + (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
}

/**
 * 计算文件大小
 * @return {number}                   文件大小（字节）
 */
export function size2Str(size) {
	if (size < 1024) {
		return size + 'B';
	} else if (size >= 1024 && size < Math.pow(1024, 2)) {
		return parseFloat(size / 1024).toFixed(2) + 'KB';
	} else if (size >= Math.pow(1024, 2) && size < Math.pow(1024, 3)) {
		return parseFloat(size / Math.pow(1024, 2)).toFixed(2) + 'MB';
	} else if (size > Math.pow(1024, 3)) {
		return parseFloat(size / Math.pow(1024, 3)).toFixed(2) + 'GB';
	} else {
		return 0 + 'B';
	}
}

/** 把文件转换为文件流 */
export function getFileFromUrl(url, fileName) {
	return new Promise((resolve, reject) => {
		let blob = null;
		let xhr = new XMLHttpRequest();
		xhr.open("GET", url);
		xhr.setRequestHeader('Accept', 'application/vnd.android.package-archive');
		xhr.responseType = "blob";
		// 加载时处理
		xhr.onload = () => {
			// 获取返回结果
			blob = xhr.response;
			let file = new File([blob], fileName, {
				type: 'application/vnd.android.package-archive'
			});
			// 返回结果
			resolve(file);
		};
		xhr.onerror = (e) => {
			reject(e)
		};
		// 发送
		xhr.send();
	});
}